module.exports = function ({Settings}) {

    /**
     * 转换成Map结构 V2
     */
    Settings.findToMapV2 = function (parse) {
        return Settings.findAll({
            order: [['id','asc']],
            attributes: {exclude: ['created_at', 'updated_at']},
            raw: true
        }).then(function (rows) {
            let settings = {};
            for (let i in rows) {
                let row = rows[i];
                let value = row.value, name = row.name;
                if (parse) {
                    let type = row.type.toLowerCase();
                    switch (type) {
                        case 'int':
                        case 'interger':
                            value = parseInt(value);
                            break;
                        case 'float':
                        case 'decimal':
                        case 'double':
                            value = parseFloat(value)
                            break;
                        case 'boolean':
                            value = parseInt(value) ? true : false;
                            break;
                        case 'json':
                            try {
                                value = JSON.parse(value);
                            } catch (e) {
                                console.error('json 格式错误:', name);
                                value = {};
                            }
                            break;
                    }
                }
                settings[name] = value;
            }
            return settings;
        });
    }
}